package Bizlogic;

import java.util.ArrayList;
import java.util.List;


import Model.Employee;
import Model.Order;
import Model.OrderDetail;
import Model.ProductsSearchbyCriteria;
import DAO.DAOException;
import DAO.DAOFactory;
import DAO.EmployeeDAO;
import DAO.OrderDAO;
import DAO.OrderDetailDAO;
import DAO.ProductsDAO;
import Model.Product;

public class CAManager {
private ProductsDAO productsDAO;
private EmployeeDAO employeeDAO;
private OrderDAO orderDAO;
private OrderDetailDAO orderDetailDAO;
public CAManager() {
	productsDAO = DAOFactory.loadInstance().getProductsDAO();
	employeeDAO= DAOFactory.loadInstance().getEmployeeDAO();
	orderDetailDAO=DAOFactory.loadInstance().getOrderDetailDAO();
	orderDAO=DAOFactory.loadInstance().getOrderDAO();
}


	

@SuppressWarnings("finally")
public Product findProduct(int partnumber){
	Product currentproduct = new Product();
	try {
		currentproduct = productsDAO.findProduct(partnumber);
	} catch (DAOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} finally
	{
		return currentproduct;
	}
	
}
@SuppressWarnings("finally")
public ArrayList<Product> findAllProducts() {
	ArrayList<Product> currentList = new ArrayList<Product>();
	try {
		currentList = productsDAO.findAllProduct();
	} catch (DAOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} finally
	{
		return currentList;
	}
}
public void insertProducts(Product P) {
	try {
		productsDAO.insertProduct(P);
	} catch (DAOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
public void updateProduct(Product P) {
	try {
		productsDAO.updateProduct(P);
	} catch (DAOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
public void deleteProduct(Product P) {
	try {
		productsDAO.deleteProduct(P);
	} catch (DAOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
}
public ArrayList<Product> findProductsByCriteria(Product p) throws DAOException
{
	return productsDAO.findProductsByCriteria(p);
}
@SuppressWarnings("finally")
public Employee findEmployee(String name){
	Employee currentemployee= new Employee();
	try{
		currentemployee=employeeDAO.findEmployee(name);
	
	}catch (DAOException e){
		e.printStackTrace();
	}finally{
	return currentemployee;
	}
}
@SuppressWarnings("finally")
public ArrayList<Employee> findAllEmployee(){
	ArrayList<Employee> currentList = new ArrayList<Employee>();
	try {
		currentList = employeeDAO.findAllEmployee();
	} catch (DAOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} finally
	{
		return currentList;
	}
	
}
public void insertEmployee(Employee emp){
	try {
		employeeDAO.insertEmployee(emp);
	} catch (DAOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
public void deleteEmployee(String loginname) {
	try {
		employeeDAO.deleteEmployee(loginname);
	} catch (DAOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
@SuppressWarnings("finally")
public OrderDetail getOrderDetailByOrderId(int id){
OrderDetail currentorder= new OrderDetail();
	try{
		currentorder=orderDetailDAO.getOrderDetailByOrderId(id);
	
	}catch (DAOException e){
		e.printStackTrace();
	}finally{
	return currentorder;
	}
	
}

public List<OrderDetail> getAllOrderDetail(){
	
	List<OrderDetail> currentList = new ArrayList<OrderDetail>();
	try {
		currentList = orderDetailDAO.getAllOrderDetail();
		System.out.print(currentList.size());
	} catch (DAOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} 
	
		return currentList;
	
}

public void inserOrderDetail(int orderid,int partnum,int quantity){

try{
	orderDetailDAO.inserOrderDetail(orderid, partnum, quantity);
}catch (DAOException e){
	e.printStackTrace();	
      }

    }

public Order getOrderByOrderId(int id){
	Order currentOrder= new Order();
	try{
		currentOrder= orderDAO.getOrderByOrderId(id);
	}catch(DAOException e){
		e.printStackTrace();
	}
		return currentOrder;
	
}
public void insertNewOrder(Order order){
	
	try {
		orderDAO.insertNewOrder(order);
	} catch (DAOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}	
	}

public ArrayList<Product> findProductslessStock(){
	ArrayList<Product> currentList = new ArrayList<Product>();
	try {
		currentList = productsDAO.findProductlessStock();
	} catch (DAOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} 
		return currentList;
	
	
}
}


